package com.happycat.controller.social;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happycat.base.R;
import com.happycat.social.dto.SocialVoiceRoomWheatSaveDto;
import com.happycat.social.entity.SocialVoiceRoomWheat;
import com.happycat.social.service.ISocialVoiceRoomWheatService;
import com.happycat.utils.ObjectsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author shenbin
 * @CreatedDate 2020-09-10
 * @Modified By:
 */
@RestController
@RequestMapping("/social/room/wheat")
@Api(tags = "API-聊天室-麦位管理", value = "聊天室")
public class SocialRoomWheatController {
    @Autowired
    private ISocialVoiceRoomWheatService socialVoiceRoomWheatService;

    @ApiOperation(value = "新增", notes = "操作权限：房主; 最大限制8个麦位(不含主持位)")
    @PostMapping(value = "/add")
    public R<SocialVoiceRoomWheat> add(@RequestBody @Valid SocialVoiceRoomWheatSaveDto socialVoiceRoomMemberSaveDto){
        return R.success(socialVoiceRoomWheatService.add(socialVoiceRoomMemberSaveDto));
    }

    @ApiOperation(value = "批量删除", notes = "操作权限: 房主; 主持位或被锁定麦位不可删除, 当前麦位有人则o不可删除")
    @PostMapping(value = "/del/{ids}")
    @ApiImplicitParam(name = "ids", value = "主键id，多个id可逗号分隔", required = true)
    public R<Boolean> del(@PathVariable @NotBlank String ids){
        ObjectsUtil.requireNonNull(ids, "麦位不能为空");

        return R.success(socialVoiceRoomWheatService.del(ids));
    }

    @ApiOperation(value = "查询列表-all", notes = "查询全部麦位(含主持位), 按序号升序")
    @GetMapping(value = "/getAllList")
    @ApiImplicitParam(name = "svrId", value = "房间主键id", required = true)
    public R<List<SocialVoiceRoomWheat>> getAllList(@RequestParam @NotNull(message = "房间不能为空") Long svrId){
        ObjectsUtil.requireNonNull(svrId, "房间不能为空");

        SocialVoiceRoomWheat wheat = new SocialVoiceRoomWheat();
        wheat.setSvrId(svrId);
        return R.success(socialVoiceRoomWheatService.list(new QueryWrapper<>(wheat).orderByAsc("sort")));
    }

    @ApiOperation(value = "获取麦位信息", notes = "查询指定麦位信息")
    @GetMapping(value = "/{id}/get")
    @ApiImplicitParam(name = "id", value = "麦位主键id", required = true)
    public R<SocialVoiceRoomWheat> get(@PathVariable @NotNull Long id){
        return R.success(socialVoiceRoomWheatService.getById(id));
    }

    @ApiOperation(value = "锁定/解锁麦位", notes = "操作权限: 房主，管理员，主持人；主持位不可锁定，当前麦位有人则不可锁定")
    @PostMapping(value = "/lockOrRelease")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主键id", required = true),
        @ApiImplicitParam(name = "forbid", value = "锁定/解锁: 1锁定，2解锁", required = true)
    })
    public R<SocialVoiceRoomWheat> lockOrRelease(@RequestParam @NotNull(message = "麦位不能为空") Long id,
                                                 @RequestParam @NotNull(message = "动作参数不能为空") @Range(min = 1, max = 2, message = "参数值非法") Integer forbid){
        ObjectsUtil.requireNonNull(id, "麦位不能为空");
        ObjectsUtil.requireNonNull(forbid, "动作参数不能为空");

        return R.success(socialVoiceRoomWheatService.lockOrRelease(id, forbid));
    }

    @ApiOperation(value = "更新麦位信息-上麦/下麦", notes = "操作权限: 房主，管理员，主持人; 上下麦操作,该接口不可操作主持人位,上麦操作时若当前麦位有人则不可操作, 同一个人不能占用多个麦位, 成功将返回当前麦位信息")
    @PostMapping(value = "/upOrDown")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主键id", required = true),
        @ApiImplicitParam(name = "upOrDown", value = "上麦/下麦: 1上麦，2下麦", required = true),
        @ApiImplicitParam(name = "accountId", value = "平台用户主键id", required = true)
    })
    public R<SocialVoiceRoomWheat> upOrDown(@RequestParam @NotNull Long id,
                                            @RequestParam @NotNull Long accountId,
                                            @RequestParam @NotNull @Range(min = 1, max = 2, message = "参数值非法") Integer upOrDown){
        ObjectsUtil.requireNonNull(id, "麦位不能为空");
        ObjectsUtil.requireNonNull(accountId, "用户不能为空");
        ObjectsUtil.requireNonNull(upOrDown, "动作参数不能为空");

        return R.success(socialVoiceRoomWheatService.upOrDown(id, accountId, upOrDown));
    }

}
